<?php

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Knowledges;

class KnowledgesController extends Controller
{
    //
    public function getKnowledges(Request $request){
        $knowledges = Knowledges::where('type',$request->get('type')) ->orderBy('id')-> get();
        return response([
            'data' => $knowledges,
            'code' => 200
        ]);
    }

    public function addKnowledges(Request $request){
//        $new = News::where('type',$request->get('type'));
        $data = $request->data;
//        dd($data['add']);
        foreach($data['add']??[] as $key => $item){
            Knowledges::create([
                'title' => $item['title'],
                'author' => $item['author'],
                'centent' => $item['centent'],
                'type' => $request->get('type'),
                'status' => $item['status'],
            ]);
        }
        Knowledges::destroy(array_pluck($data['remove']??[],'id'));
        foreach($data['update']??[] as $key => $item){
            Knowledges::find($item['id'])->update([
                'title' => $item['title'],
                'author' => $item['author'],
                'centent' => $item['centent'],
                'type' => $request->get('type'),
                'status' => $item['status'],
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }
}
